﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StudentsDB
{
    public partial class Address_Classifier : Form
    {
        public Address_Classifier()
        {
            InitializeComponent();
        }
        private FileInfo tmp;
        private string connectionString;
        private void button1_Click(object sender, EventArgs e)
        {
             if (openFileDialog1.ShowDialog() == DialogResult.OK)
             {
                 AddrPath.Text = openFileDialog1.FileName;
             }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreetPath.Text = openFileDialog1.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                HousePath.Text = openFileDialog1.FileName;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                AbbrPath.Text = openFileDialog1.FileName;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            AddrPath.Text = ""; AbbrPath.Text = ""; StreetPath.Text = ""; HousePath.Text = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            /*
             * Загрузил данные из dbf-файла в dataGridView:
string strConString = @"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=c:\;";
System.Data.Odbc.OdbcConnection dbfConnection = new OdbcConnection(strConString);
string strCommand = "SELECT * FROM table1";
System.Data.Odbc.OdbcDataAdapter dbfAdapter = new OdbcDataAdapter();
dbfAdapter.SelectCommand = new OdbcCommand(strCommand, dbfConnection);
DataTable dbfDataset = new DataTable();
dataGridView1.DataSource = dbfDataset;
dbfConnection.Open();
dbfAdapter.Fill(dbfDataset);
dbfConnection.Close();
dataGridView1.AutoResizeColumns();

             */
            /*
             string dbDir = @"C:\db\";
             string file = "file.dbf";
             string connectionString = String.Format("Driver={{Microsoft dBase Driver (*.dbf)}};SourceType=DBF;SourceDB={0};Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;", dbDir);
             using(OdbcConnection connection = new OdbcConnection(connectionString)){    
             connection.Open();    
             using (OdbcCommand cmd = connection.CreateCommand())    {        
             cmd.CommandText = @"SELECT * FROM " + dbDir + file;        
             DataTable dt = new DataTable();        
             dt.Load(cmd.ExecuteReader());    
             }
             }
             */
            tmp = new FileInfo(openFileDialog1.FileName);
            connectionString = String.Format("Driver={{Microsoft dBase Driver (*.dbf)}};SourceType=DBF;SourceDB={0};Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;", tmp.DirectoryName);
            //richTextBox1.Text = tmp.DirectoryName;
            using (OdbcConnection connection = new OdbcConnection(connectionString))
            {
                /*if (connection == null) richTextBox1.Text = "connection is null!!!";
                else
                {
                    richTextBox1.Text = "connection is not null!!!";
                }*/
                string strCommand = @"SELECT * FROM KLADR";
                OdbcDataAdapter dbfAdapter = new OdbcDataAdapter();
                dbfAdapter.SelectCommand = new OdbcCommand(strCommand, connection);
                DataTable dbfDataset = new DataTable();
                dataGridView1.DataSource = dbfDataset;
                connection.Open(); 
                
                dbfAdapter.Fill(dbfDataset);
                connection.Close();
                dataGridView1.AutoResizeColumns();
                /*connection.Open(); 
                using (OdbcCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"SELECT * FROM D:\Kate\Documents\учёба\БД студентов\KLDR\KLADR.DBF"; //+AddrPath.Text; 
                    DataTable dt = new DataTable(); 
                    dt.Load(cmd.ExecuteReader());
                    //dt.Columns.Count;
                    richTextBox1.Text= dt.Columns.ToString();
                }*/
            }
        }
    }
}
